#include "formhsxm.h"
#include "ui_formhsxm.h"
#include <mysql.h>
#include <QClipboard>

FormHSXM::FormHSXM(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormHSXM)
{
    ui->setupUi(this);
    this->setWindowTitle("核算项目设置");
    this->show();
    RefTree();
    QString SQL="";
    QSqlQuery query(mySQL::cn);
    SQL="SELECT nd FROM  nd ";
    SQL+=" order by nd desc ";
    ui->cmbND->clear();
    query.exec(SQL);
    while(query.next()){
        ui->cmbND->addItem(query.value("nd").toString());
    }
    query.clear();
}

FormHSXM::~FormHSXM()
{
    delete ui;
}

void FormHSXM::RefTree()
{
    ui->treeWidget->clear();
    ui->treeWidget->setHeaderLabels(QStringList() << "项目代码" << "项目简称"  << "项目名称" << "助记码" );
    ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    //ui->treeWidget->setStyleSheet("QTreeWidget::item{border-right: 1px solid blue;border-left: 1px solid blue;border-bottom: 1px solid blue;}"); //加表格线
    QString SQL="";
    QStringList child;
    SQL=QString("SELECT xmdm,xmmc,xmjc,xmzj FROM `hsxm` WHERE type_id='%1' and nd='%2' order by xmdm").arg(mySQL::TypeId).arg(mySQL::ND);
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("xmdm").toString().trimmed();
         child.clear();
         child << query.value("xmdm").toString();
         child << query.value("xmjc").toString();
         child << query.value("xmmc").toString();
         child << query.value("xmzj").toString();
         QTreeWidgetItem * citem =new QTreeWidgetItem(child);
         if (str.length() ==1){
             ui->treeWidget->addTopLevelItem(citem);
         }else{
             //遍历找出父节点
             bool f=false;
             QTreeWidgetItemIterator it(ui->treeWidget);
                 while (*it) {
                      //(*it)->text(0);
                     if ((*it)->text(0)==str.left(str.length()-1)){
                         (*it)->addChild(citem);
                         f=true;
                     }
                     ++it;
                 }
                 if(f==false){
                     //如果减1个字符没照到上级 就减2个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-2)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
                 if(f==false){
                     //如果减2个字符没照到上级 就减3个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-3)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
         }
    }
}

void FormHSXM::on_btnSave_clicked()
{
    QString SQL="";
    QString XMDM="";
    QString XMMC="";

    QString XMJC=ui->txtXMJC->text().trimmed();
    QString XMZJ=ui->txtXMZJ->text().trimmed();
    QString XMBM1=ui->txtBM1->text().trimmed();
    QString XMBM2=ui->txtBM2->text().trimmed();
    QString XMBZ=ui->txtBZ->text().trimmed();
    QString ND=mySQL::ND;
    QString Idd="";
    bool Ssign=false;
    bool Tsign=false;
    XMDM=ui->txtXMDM->text().trimmed();
    XMMC=ui->txtXMMC->text().trimmed();
    if (XMDM==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT idd FROM `hsxm` WHERE xmdm='%1' and type_id='%2' and nd='%3'").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    Ssign=false;
    while(query.next()){
        Ssign=true;
        Idd=query.value("idd").toString();
        //QMessageBox::warning(this,"警告","idd="+Idd);
    }
    Tsign=true;
    if (Ssign==false && XMDM.length()>1){
        //新建 并 科目代码长度小于1  就判断是否存在上级科目
        int kl=XMDM.length()-1;
        SQL=QString("SELECT idd FROM `hsxm` WHERE  type_id='%1' and nd='%2' and (xmdm='%3' ").arg(mySQL::TypeId).arg(ND.trimmed()).arg(XMDM.left(kl).trimmed());
        if (kl>=2){
            //兼容2位科目代码
            kl--;
            SQL+=QString(" or xmdm='%1'").arg(XMDM.left(kl));
        }
        SQL+= ")";
        query.exec(SQL);
        Tsign=false;
        while(query.next()){
            Tsign=true;
        }
    }
    if (Tsign==true){
        //开始准备保存科目代码
        SQL="";
        if (Ssign==false ){
            SQL="INSERT INTO hsxm(type_id,nd,xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz) VALUES (";
            SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMDM.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMJC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMZJ.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMBM1.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMBM2.trimmed().replace("'","''"));
            SQL+=QString("'%1')").arg(XMBZ.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }else{
            SQL="update hsxm set ";
            SQL+=QString("ND='%1',").arg(ND.trimmed().replace("'","''"));
            SQL+=QString("xmdm='%1',").arg(XMDM.trimmed().replace("'","''"));
            SQL+=QString("xmmc='%1',").arg(XMMC.trimmed().replace("'","''"));
            SQL+=QString("xmjc='%1',").arg(XMJC.trimmed().replace("'","''"));
            SQL+=QString("xmzj='%1',").arg(XMZJ.trimmed().replace("'","''"));
            SQL+=QString("bm1='%1',").arg(XMBM1.trimmed().replace("'","''"));
            SQL+=QString("bm2='%1',").arg(XMBM2.trimmed().replace("'","''"));
            SQL+=QString("bz='%1' ").arg(XMBZ.trimmed().replace("'","''"));
            SQL+=QString("where idd=%1").arg(Idd.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }
        if (SQL!=""){
            query.exec(SQL);
            QMessageBox::information(this,"信息","保存成功！");
            RefTree();
        }

    }else{
        QMessageBox::warning(this,"警告","不存在上级项目代码 无法保存！！");
    }
    RefTree();
}



void FormHSXM::on_btnClose_clicked()
{
    this->close();
}

void FormHSXM::on_btnDel_clicked()
{
    QString SQL="";
    QString XMDM="";
    QString XMMC="";
    QString ND=mySQL::ND;
    bool delft=false;
    XMDM=ui->txtXMDM->text().trimmed();
    XMMC=ui->txtXMMC->text().trimmed();
    SQL="确认删除这条记录吗？ 删除后是无法恢复的！！！！";
    if (QMessageBox::information(NULL, "提醒", SQL,QMessageBox::Yes | QMessageBox::No, QMessageBox::No)==QMessageBox::No){
        return;
    }
    SQL="";
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT count(xmdm) as t  FROM `hsxm` WHERE xmdm like '%1%' and type_id='%2' and nd='%3' ").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    SQL="";
    while(query.next()){
        if (query.value("t").toString().trimmed()=="1"){

            delft=true;
        }else{
            QMessageBox::information(this,"提醒","存在下级科目，不得删除！！");
            return;
        }

    }
    if (delft==true){
        SQL=QString("delete  FROM `hsxm` WHERE xmdm = '%1' and type_id='%2' and nd='%3'").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND);
        query.exec(SQL);
        RefTree();
        QMessageBox::information(this,"提醒","删除成功");

    }

}

void FormHSXM::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    QString SQL="";
    QString ND=mySQL::ND;
    QString XMDM=item->text(0);
    SQL="";
    int i=column;
    i++;
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz  FROM `hsxm` WHERE xmdm = '%1' and type_id='%2' and nd='%3' ").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    SQL="";
    ui->txtXMDM->setText("");
    ui->txtXMMC->setText("");
    ui->txtXMJC->setText("");
    ui->txtXMZJ->setText("");
    ui->txtBM1->setText("");
    ui->txtBM2->setText("");
    ui->txtBZ->setText("");
    while(query.next()){
        ui->txtXMDM->setText(query.value("xmdm").toString().trimmed());
        ui->txtXMMC->setText(query.value("xmmc").toString().trimmed());
        ui->txtXMJC->setText(query.value("xmjc").toString().trimmed());
        ui->txtXMZJ->setText(query.value("xmzj").toString().trimmed());
        ui->txtBM1->setText(query.value("bm1").toString().trimmed());
        ui->txtBM2->setText(query.value("bm2").toString().trimmed());
        ui->txtBZ->setText(query.value("bz").toString().trimmed());
    }
}

void FormHSXM::on_btnChild_clicked()
{
    QString SQL="";
    QString XMDM="";
    QString XMMC="";

    ui->txtXMMC->setText("");
    ui->txtXMJC->setText("");
    ui->txtXMZJ->setText("");
    ui->txtBM1->setText("");
    ui->txtBM2->setText("");
    ui->txtBZ->setText("");
    QString XMJC=ui->txtXMJC->text().trimmed();
    QString XMZJ=ui->txtXMZJ->text().trimmed();
    QString XMBM1=ui->txtBM1->text().trimmed();
    QString XMBM2=ui->txtBM2->text().trimmed();
    QString XMBZ=ui->txtBZ->text().trimmed();
    QString ND=mySQL::ND;
    QString Idd="";

    XMDM=ui->txtXMDM->text().trimmed();
     if (XMDM==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(xmdm) as t  FROM `hsxm` WHERE (xmdm like '%1_' or xmdm='%1') and type_id='%2' and nd='%3'").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    //qDebug() << SQL;
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        //QMessageBox::warning(this,"警告",str);
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==XMDM){
            //如果没有下级科目就添加第一个项目
            XMDM+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                XMDM+=(QChar)K_ascii;
        }
    }
    XMMC="新编项目";
    SQL="INSERT INTO hsxm(type_id,nd,xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz) VALUES (";
    SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMDM.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMMC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMJC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMZJ.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBM1.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBM2.trimmed().replace("'","''"));
    SQL+=QString("'%1')").arg(XMBZ.trimmed().replace("'","''"));
    query.exec(SQL);
    RefTree();
    QTreeWidgetItemIterator it(ui->treeWidget);
    int itemCount=0;
    while (*it) {
         //(*it)->text(0);
        if  ((*it)->text(0).left(XMDM.length())==XMDM){
            itemCount++;
        }
        if  (XMDM.left((*it)->text(0).length())==(*it)->text(0)){
            (*it)->setExpanded(true);
        }else{
            (*it)->setExpanded(false);
        }
        if ((*it)->text(0)==XMDM){
            (*it)->setSelected(true);
            FormHSXM::on_treeWidget_itemClicked((*it), 0);
            ui->treeWidget->scrollToItem((*it));
        }else{
            (*it)->setSelected(false);
        }
        ++it;
    }
    QMessageBox::information(this,"提醒","新项目代码为："+XMDM);
}

void FormHSXM::on_btnJuxtaposition_clicked()
{
    QString SQL="";
    QString XMDM="";
    QString XMMC="";
    ui->txtXMMC->setText("");
    ui->txtXMJC->setText("");
    ui->txtXMZJ->setText("");
    ui->txtBM1->setText("");
    ui->txtBM2->setText("");
    ui->txtBZ->setText("");
    QString XMJC=ui->txtXMJC->text().trimmed();
    QString XMZJ=ui->txtXMZJ->text().trimmed();
    QString XMBM1=ui->txtBM1->text().trimmed();
    QString XMBM2=ui->txtBM2->text().trimmed();
    QString XMBZ=ui->txtBZ->text().trimmed();
    QString ND=mySQL::ND;
    QString Idd="";

    XMDM=ui->txtXMDM->text().trimmed();
    XMDM=XMDM.left(XMDM.length()-1);
     if (XMDM==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(xmdm) as t  FROM `hsxm` WHERE (xmdm like '%1_' ) and type_id='%2' and nd='%3'").arg(XMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
   //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        //QMessageBox::warning(this,"警告",str);
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==XMDM){
            //如果没有下级科目就添加第一个项目
            XMDM+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    //qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                XMDM+=(QChar)K_ascii;
        }
    }
    XMMC="新编项目";
    SQL="INSERT INTO hsxm(type_id,nd,xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz) VALUES (";
    SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMDM.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMMC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMJC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMZJ.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBM1.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBM2.trimmed().replace("'","''"));
    SQL+=QString("'%1')").arg(XMBZ.trimmed().replace("'","''"));
    query.exec(SQL);
    RefTree();
    QTreeWidgetItemIterator it(ui->treeWidget);
    int itemCount=0;
    while (*it) {
         //(*it)->text(0);
        if  ((*it)->text(0).left(XMDM.length())==XMDM){
            itemCount++;
        }
        if  (XMDM.left((*it)->text(0).length())==(*it)->text(0)){
            (*it)->setExpanded(true);
        }else{
            (*it)->setExpanded(false);
        }
        if ((*it)->text(0)==XMDM){
            (*it)->setSelected(true);
            FormHSXM::on_treeWidget_itemClicked((*it), 0);
            ui->treeWidget->scrollToItem((*it));
        }else{
            (*it)->setSelected(false);
        }
        ++it;
    }
    QMessageBox::information(this,"提醒","新项目代码为："+XMDM);
}

void FormHSXM::on_btnPaste_clicked()
{
    //核算项目粘贴
    QMessageBox message(QMessageBox::Warning,"请确认","确认粘贴数据的格式为 \n[项目代码]，[名称]?",QMessageBox::Yes|QMessageBox::No,NULL);
        if (message.exec()==QMessageBox::Yes)
        {
            QClipboard *board = QApplication::clipboard();
            QString str = board->text();
            //QMessageBox::information(NULL, "From clipboard", str);
            QStringList strList = str.split("\n");
            QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
            for(int i = 0; i < strList.size()-1; i++) {
                QStringList subList = strList[i].split("\t");


                QString SQL="";
                QString XMDM=subList[0].trimmed();
                QString XMMC=subList[1].trimmed();

                QString XMJC="";
                QString XMZJ="";
                QString XMBM1="";
                QString XMBM2="";
                QString XMBZ="粘贴导入";
                QString ND=mySQL::ND;

                //qDebug()<<subList[0]+"-"+subList[1];
                SQL="INSERT INTO hsxm(type_id,nd,xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz) VALUES (";
                SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMDM.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMMC.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMJC.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMZJ.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMBM1.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMBM2.trimmed().replace("'","''"));
                SQL+=QString("'%1')").arg(XMBZ.trimmed().replace("'","''"));
                //qDebug()<<SQL;
                query.exec(SQL);
            }
            RefTree();
            //QMessageBox::information(NULL, "From clipboard", str);
        }
        else
        {
            qDebug()<<"clicked no\n";
        }

}

void FormHSXM::on_btnIMP_clicked()
{


    QString SQL="";
    QSqlQuery query(mySQL::cn);
    QString ND=ui->cmbND->currentText();


    SQL=QString("SELECT xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz FROM `hsxm` WHERE type_id='%1' and nd='%2' order by xmdm").arg(mySQL::TypeId).arg(ND);
    query.exec(SQL);
    while(query.next()){
        QString XMDM=query.value("xmdm").toString().trimmed();
        QString XMMC=query.value("xmmc").toString().trimmed();

        QString XMJC=query.value("xmjc").toString().trimmed();
        QString XMZJ=query.value("xmzj").toString().trimmed();
        QString XMBM1=query.value("bm1").toString().trimmed();
        QString XMBM2=query.value("bm2").toString().trimmed();
        QString XMBZ=query.value("bz").toString().trimmed();
        QString ND=mySQL::ND;

        SQL="INSERT INTO hsxm(type_id,nd,xmdm,xmmc,xmjc,xmzj,bm1,bm2,bz) VALUES (";
        SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMDM.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMMC.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMJC.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMZJ.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMBM1.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMBM2.trimmed().replace("'","''"));
        SQL+=QString("'%1')").arg(XMBZ.trimmed().replace("'","''"));
        //qDebug()<<SQL;
        mySQL::cn.exec(SQL);
    }
    RefTree();


}
